// pagesWorkOrder/workOrderMyIndexHandle/components/transfer/transfer.js 
const {
  WorkOrderMyIndexApi
} = require('../../../../common/apis/workOrder')
const mixin_formItemType = require('../../../../common/mixins/formItemType')
const {
  DIC_KEY
} = require('../../../../config/dicKey.config')

const {
  getDicList
} = require('../../../../utils/dic')
const {
  WxValidate
} = require('../../../../utils/WxValidate')

Component({
  behaviors: [mixin_formItemType],

  /**
   * 组件的属性列表
   */
  properties: {
    orderNum: String
  },

  /**
   * 组件的初始数据
   */
  data: {
    //  转单原因
    reason: '',
    //  转单说明
    remark: '',
    //  派单对象标识
    sendObjId: '',
    //  派单对象类型 1 用户 2小组 3部门
    sendObjType: '',
    //  派单所属小组
    sendTeam: '',
    reasonList: getDicList(DIC_KEY.obs_trans_reason),
    rules: {
      sendObjType: {
        required: true,
      },
      reason: {
        required: true,
      },
    },
    message: {
      sendObjType: {
        required: '请选择转交',
      },
      reason: {
        required: '请选择转交原因',
      }
    }

  },

  /**
   * 组件的方法列表
   */
  methods: {
    submit(callback) {
      const {
        reason,
        remark,
        sendObjId,
        sendObjType,
        sendTeam,
        orderNum
      } = this.data
      const params = {
        orderNum,
        reason,
        remark,
        sendObjId,
        sendObjType,
        sendTeam
      }
      const validate = new WxValidate(this.data.rules, this.data.message)

      if (!validate.checkForm(params)) {
        const error = validate.errorList[0]
        wx.showToast({
          title: error.msg,
          icon: 'error'
        })
        return false
      }
      WorkOrderMyIndexApi.transferOrder(params, ({
        code,
        msg
      }) => {
        if (code === 200) {
          wx.showToast({
            title: '提交成功',
            icon: "success"
          })
          wx.$_.delay(callback, 1000)

        } else {
          wx.showToast({
            title: msg,
            icon: 'error'
          })
        }
      })
    },
    changeRadioGroup(e) {
      this.setData({
        reason: e.detail.value
      })
    },

    onSelectDept({
      detail
    }) {
      const type = {
        "team": 2,
        "user": 1
      }
      this.setData({
        sendObjType: type[detail.value.key],
        sendObjId: detail.value.value,
        sendTeam: type[detail.value.key] === 1 ? detail.value.teamId : ''
      })
    }
  }
})